<%- include ('header.ejs') %>
<style>
  .modal {
    display: none;
    /* 默认隐藏 */
    position: fixed;
    /* 固定定位 */
    z-index: 1;
    /* 设置在顶层 */
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    overflow: auto;
    background-color: rgb(0, 0, 0);
    background-color: rgba(0, 0, 0, 0.4);
  }

  /* 弹窗内容 */
  .modal-content {
    background-color: #fefefe;
    margin: 15% auto;
    padding: 20px;
    border: 1px solid #888;
    width: 80%;
  }

  /* 关闭按钮 */
  .close {
    color: #aaa;
    float: right;
    font-size: 28px;
    font-weight: bold;
  }

  .close:hover,
  .close:focus {
    color: black;
    text-decoration: none;
    cursor: pointer;
  }
</style>

<h3>console管理</h3>
<span>创建一个公司，刷新后创建第二家公司</span>

<div>
  <div style="display: flex; flex-direction: column; width: 40%;">
    <button id="myBtn">创建公司</button>
    <span id="company"></span>
  </div>

  <div style="display: flex; flex-direction: column; width: 40%;">
    <button id="create_user">创建新公司管理员</button>
    <span id="user"></span>
  </div>

</div>


<!-- 弹窗 -->
<div id="myModal" class="modal">

  <!-- 弹窗内容 -->
  <div class="modal-content">
    <div><span>创建公司</span>
      <span class="close">&times;</span>
    </div>

    <form id='test_form' action='' method='post' style="display: flex;flex-direction: column;"
      omsubmit='return checkForm()'>
      <label for="companyName">公司名称</label>
      <input type="text" name="companyName" id="companyName" class="text ui-widget-content ui-corner-all" />
      <label for="companyCap">公司英文缩写</label>
      <input type="text" name="companyCap" id="companyCap" value="" class="text ui-widget-content ui-corner-all" />
      <label for="companyNum">公司人数</label>
      <input type="number" name="companyNum" id="companyNum" value="" class="text ui-widget-content ui-corner-all" />
    </form>
    <button type="submit" id="submit">创建公司</form>
  </div>

</div>







<script>
  var company = null;
  // 点击按钮打开弹窗
  $("#myBtn").on("click", function () {
    $("#myModal").css({
      display: "block"
    });
  });
  // 点击 <span> (x), 关闭弹窗
  $(".close").on("click", function () {
    $("#myModal").css({
      display: "none"
    });
  });
  $("#submit").on("click", function () {
    $.post("http://localhost:8013/rest/companys/create", {
      companyName: $("#companyName").val(),
      companyCap: $("#companyCap").val(),
      companyNum: $("#companyNum").val()
    }, function (data, status) {
      if (data.code === 200) {
        company = data.data;
        $("#company").text(`${company.companyName}创建成功 /r/n 具体数据如下：${JSON.stringify(company)}`);
      }
      $("#myModal").css({
        display: "none"
      });
    })
  });
  $("#create_user").on("click", function () {
    $.post("http://localhost:8013/rest/user/reg", {
      name: "admin",
      password: "BD535B67AB19A5026A67F4C7592BDE63",
      companyId: company._id,
    }, function (data, status) {
      if (data.code === 200) {
        var user = data.data;
        $("#user").text(`管理员${user.companyName}创建成功 /r/n 具体数据如下：${JSON.stringify(user)}`);
      }
    })
  });
</script>

<%- include('footer.ejs')%>